package www.rlicker.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.SysUser;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.ISysUserService;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

@Controller  
@RequestMapping("/sysuser")  
public class UserController {
	
	@Autowired
	private ISysUserService service;
	
	/**
	 * 账号列表页面
	 * @return
	 */
	@GetMapping("/showlist")
	public String showlist(){
		return "system/userList";
	}
	
	/**
	 * 获取账号列表数据
	 * @param request
	 * @return
	 */
	@GetMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
	        Criteria<SysUser> param = new Criteria<SysUser>();
	        param.setStartIndex(Integer.parseInt(request.getParameter("pageNumber")));
	        if (!StringUtil.isNullOrBlank(request.getParameter("merchantId"))) {
				param.addParam("merchantId", request.getParameter("merchantId"));
			}
	        List<Map<String,Object>> list = service.queryPage(param);
	        PageResult pageResult = new PageResult();
			pageResult.setTotal(service.queryPageCount(param));
			pageResult.setRows(list);
			return pageResult;	
	}
	
	/**
	 * 修改 - 新增页面
	 * @param id
	 * @return
	 */
	@GetMapping("/detail")
	public String detail(String id,ModelMap map){
		if (!StringUtil.isNullOrBlank(id)) {
			SysUser sysUser = service.queryEntity(id);
			map.put("sysUser", sysUser);
		}
		return "userDetail";
	}
	
	/**
	 * 根据id删除账号
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public JsonResult delete(String id){
		String byId = service.deleteById(id);
		return null;
	}

}
